﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ScarfMaker
{
    /// <summary>
    /// ScarfWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ScarfWindow : Window
    {
        private FileInfo fiTuAn = null;
        private FileInfo fiDiWen = null;
        private FileInfo fiBianKuang = null;
        public ScarfWindow()
        {
            InitializeComponent();
            spMenu.Children.Clear();
            DirectoryInfo diTuAn = Models.StaticClass.diScarfData.GetDirectories("图案").FirstOrDefault();
            DirectoryInfo diDiWen = Models.StaticClass.diScarfData.GetDirectories("底纹").FirstOrDefault();
            DirectoryInfo diBianKuang = Models.StaticClass.diScarfData.GetDirectories("边框").FirstOrDefault();
            while(diTuAn==null|diDiWen==null||diBianKuang==null)
            {
                MessageBox.Show("目录结构错误");
                if (Login.CheckPath())
                {
                    diTuAn = Models.StaticClass.diScarfData.GetDirectories("图案").FirstOrDefault();
                    diDiWen = Models.StaticClass.diScarfData.GetDirectories("底纹").FirstOrDefault();
                    diBianKuang = Models.StaticClass.diScarfData.GetDirectories("边框").FirstOrDefault();
                }
            }
            CreateMenu(diTuAn,SetTuAn);
            CreateMenu(diDiWen,SetDiWen);
            CreateMenu(diBianKuang,SetBianKuang);
        }
        void CreateMenu(DirectoryInfo diMenuType,EventHandler Handler)
        {
            Models.vm_MaterialMenu mm = new Models.vm_MaterialMenu();
            DirectoryInfo[] di2 = diMenuType.GetDirectories();
            mm.MenuName = diMenuType.Name;
            foreach (DirectoryInfo diMaterialType in di2)
            {
                Models.MeterialType mt = new Models.MeterialType();
                mt.TypeName = diMaterialType.Name;
                FileInfo[] fi1 = diMaterialType.GetFiles();
                foreach (FileInfo fiImage in fi1)
                {
                    mt.listFileInfo.Add(new FileInfo(fiImage.FullName));
                }
                mm.listMeterialType.Add(mt);
            }
            MaterialMenu mMenu = new MaterialMenu(mm);
            mMenu.SelectingImage += Handler;
            spMenu.Children.Add(mMenu);
        }
        private void SetTuAn(object sender, EventArgs e)
        {
            string fullName = ((Image)sender).Tag as string;
            if(fiTuAn!=null&&fiTuAn.FullName==fullName)
            {
                fiTuAn = null;
            }
            else
            {
                fiTuAn =new FileInfo(fullName);
            }
            UpdatePreview();
        }
        private void SetDiWen(object sender, EventArgs e)
        {
            string fullName = ((Image)sender).Tag as string;
            if (fiDiWen != null && fiDiWen.FullName == fullName)
            {
                fiDiWen = null;
            }
            else
            {
                fiDiWen = new FileInfo(fullName);
            }
            UpdatePreview();
        }
        private void SetBianKuang(object sender, EventArgs e)
        {
            string fullName = ((Image)sender).Tag as string;
            if (fiBianKuang != null && fiBianKuang.FullName == fullName)
            {
                fiBianKuang = null;
            }
            else
            {
                fiBianKuang = new FileInfo(fullName);
            }
            UpdatePreview();
        }
        private void UpdatePreview()
        {
            imgTuAn.Source = fiTuAn == null ? null : new BitmapImage(new Uri("file://" + fiTuAn.FullName));
            imgDiWen.Source = fiDiWen == null ? null : new BitmapImage(new Uri("file://" + fiDiWen.FullName));
            imgBianKuang.Source = fiBianKuang == null ? null : new BitmapImage(new Uri("file://" + fiBianKuang.FullName));
        }

        private void btnReChose_Click(object sender, RoutedEventArgs e)
        {
            imgTuAn.Source = null;
            imgDiWen.Source = null;
            imgBianKuang.Source = null;
        }

        private void btnBackMenu_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnExitSystem_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }

        private void btnMoreMaterials_Click(object sender, RoutedEventArgs e)
        {
            Models.StaticClass.WaitForOpen = new MaterialView();
            this.Close();
        }
    }
}
